//
//  RadioHandel.m
//  iNews
//
//  Created by lanou3g on 14-12-6.
//  Copyright (c) 2014年 张在青. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>

#import "RadioHandel.h"
#import "NetRequest.h"
#import "RadioDataModel.h"
#import "radioDetailDataModel.h"
//#import "AudioStreamer.h"
static RadioHandel *radioData = nil;

@implementation RadioHandel
-(void)dealloc
{
    [_RadioDataArray release];
    [_docidString release];
    [super dealloc];
    
}

+(RadioHandel *)shareRadioData
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        radioData = [[RadioHandel alloc] init];
        radioData.RadioDataArray = [NSMutableArray array];
        //radioData.RadioTotalArray = [NSMutableArray array];
        radioData._flag = YES;
        
    });
    return radioData;
}


-(void)reqestRadioData
{
    __block RadioHandel *radioSelf = self;

    [NetRequest netReuestWithUrlString:[kRadioDataString stringByAppendingFormat:@"%ld-20.html",(long)self.JKcount] block:^(NSData *data) {
        
        NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        
        NSMutableArray *tempArray;
        //判断接口是0得时候,数组正常初始化,当不是0时,用数组初始化.
        if (0 == self.JKcount ) {
            tempArray = [NSMutableArray array];
        }else{
            tempArray = [NSMutableArray arrayWithArray:radioSelf.RadioDataArray[0]];
        }

        [self.RadioDataArray removeAllObjects];
        for (NSDictionary *dict in tempDict[@"T1379038288239"]) {
            
            RadioDataModel *rm = [[[RadioDataModel alloc] init] autorelease];
            [rm setValuesForKeysWithDictionary:dict];
            [tempArray addObject:rm];
            
        }
       [radioSelf.RadioDataArray addObject:tempArray];
       
       radioSelf.RadioTotalArray = [NSMutableArray arrayWithArray:radioSelf.RadioDataArray];

    }];
   
   // NSLog(@"刷新一次");
}



-(void)requestDetailRadioData
{
    __block RadioHandel *radioDetailSelf = self;
    
    NSString *urlString = [KRadioDetailDataString stringByAppendingString:[NSString stringWithFormat:@"%@/full.html",self.docidString]];
   // NSLog(@"u%@",urlString);
    
    [NetRequest netReuestWithUrlString:urlString block:^(NSData *data) {
        
        NSDictionary *tempDict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
           // NSLog(@"tempDict : %@",tempDict);
               //这里解析了所有的详情数据,当但是只取我们要用的
        NSMutableArray *tempArray = [NSMutableArray array];

        radioDetailDataModel *rddm =[[[radioDetailDataModel alloc] init] autorelease];
        
        NSDictionary *dic = [tempDict valueForKey:[tempDict allKeys][0]];
       
        [rddm setValuesForKeysWithDictionary:dic];
         //NSLog(@"##rddm.source_url%@",rddm.source_url);
        
//        NSDictionary *dictURl_mp4 = tempDict[@"ACE99P3V00964M3G"][@"video"][0];
        NSString *dictURl_mp4 = [[tempDict[self.docidString][@"video"] lastObject] valueForKey:@"url_mp4"];
       // NSLog(@"dictURl_mp4:%@",dictURl_mp4);
        rddm.url_mp4 =  [NSString stringWithFormat:@"%@",dictURl_mp4];
        
        NSString *bakimageCover = [[tempDict[self.docidString][@"video"] lastObject ] valueForKey:@"cover"];
        rddm.cover = bakimageCover;
        
        //NSLog(@"cover :%@",bakimageCover);
    
        [tempArray addObject: rddm];
        
        radioDetailSelf.RadioDetailDataArray = [NSMutableArray arrayWithArray:tempArray];
        
    }];
}

//播放器的方法
-(void)StarPlayerWithString:(NSString *)aString
{
//    [self.player pause];
//    self.player = nil;
    
    if (aString) {
        
        _url = [NSURL URLWithString:aString];
       
        if (!_url) {
            
              _url =[NSURL  fileURLWithPath:aString];
        }
        
        if (!self.player) {
            [self.player release];
            
            self.playerItem = [[AVPlayerItem alloc] initWithURL:_url];
            self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
            
            [self.player play];
            [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
                
                //            NSLog(@"%d",(int)CMTimeGetSeconds(self.playerItem.currentTime));
                self.progress = CMTimeGetSeconds(self.playerItem.currentTime);
                
                NSLog(@"%d",self.progress);
                self.totalTimeValue =  CMTimeGetSeconds(self.playerItem.duration);
                NSLog(@"%d",self.totalTimeValue);
                
                //[[NSNotificationCenter defaultCenter] postNotificationName:@"progress" object:nil];
            }];
            
            
        }else{
            [self.player pause];
            [self.player release];
            self.player = nil;
            self.playerItem = [[AVPlayerItem alloc] initWithURL:_url];
            self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
            [self.player play];
        }

        
    }
    
}


-(void)getCurrentTime
{
    //把自己播放器播放当前MP3的时间赋值给  属性currentTime;
   // self.currentTime = [self.player currentTime];
    //NSLog(@"%@",[self.sTreamer currentTime]);
    //[ 将Data日期转换成Nsinterge ] 把当前的播放时间赋值给Progress ;
    //self.progress = [self.sTreamer timeStringToSS:self.currentTime];
    
    
   // self.totalTimes = [self.player totalTime];
   // self._flag = NO;
    
    

    //设置一个通知中心,在这个值改变的时候接受者 重新赋值
    [[NSNotificationCenter defaultCenter] postNotificationName:@"currentTime" object:nil];
}

@end
